package com.jochen.core.ui.realize

import com.badlogic.gdx.scenes.scene2d.actions.*
import com.badlogic.gdx.scenes.scene2d.ui.*
import com.badlogic.gdx.scenes.scene2d.*
import com.jochen.core.assets.SoundAsset
import com.jochen.core.screen.GameScreen
import com.jochen.core.screen.Menu
import com.jochen.core.screen.transitions.FadeTransition
import com.jochen.core.ui.onClick
import com.jochen.core.world.GdxGame

class GameMenuView(
    private val game: GdxGame,
    private val gameScreen: GameScreen
) : Table() {
    
    private val audioManager = game.audioManager
    init {
        setFillParent(true)
        isVisible = false
        color.a = 0f
        initUi()
    }

    private fun initUi() {
        val continueGame = TextButton("继续游戏", UiStyle.getButtonStyle(game))
        val backMenu = TextButton("返回主菜单", UiStyle.getButtonStyle(game))

        continueGame.onClick {
            gameScreen.resume()
            updateAction(false)
            audioManager.playSound(SoundAsset.BUTTON_PRESS)
        }

        backMenu.onClick {
            gameScreen.setScreen(Menu::class, FadeTransition::class)
            audioManager.playSound(SoundAsset.BUTTON_PRESS)
            isVisible = false
            color.a = 0f
        }

        center()
        defaults().uniform().width(300f).height(50f)

        add(continueGame).row()
        add(backMenu)
    }
    
    fun updateAction(visible: Boolean) {
        clearActions()
        
        if(visible) {
            this.isVisible = true
            addAction(Actions.fadeIn(0.5f))
        } else {
            addAction(Actions.sequence(
            Actions.fadeOut(0.5f),
            Actions.visible(false)))
        }
    }

}